<?php  
/*

	Version   : 1.0

	Created   : 20090928

	Modified  : 20090929

	Author    : Pablo Ramirez

	co-Author :

	Descr     : Controlador de "Users"

	Changelog : 1.0 - Version Inicial
				1.1 - Se agrega mantenedor de "Users"

*/

class UsersController extends AppController 
{ 
	var $name = "Users"; 
    var $helpers = array('Html', 'Form', 'Javascript','Ajax');
    
	function index() {
		$this->checkSession();

		$this->layout = 'maintenance';

		

		if (empty($this->data)) {

		

		$users = $this->User->findAll(null,'*','usr_codigo ASC');

			foreach($users as $user) {

				$temp = array();

				foreach($user['User'] as $key=>$value) {

					if ($key == 'usr_password') {
						$temp[$key] = '***';
					} elseif ($key == 'usr_estado') {
						if ($value == 'A') {
							$value = 'Active';
						} else {
							$value = 'Locked';
						}
						$temp[$key] = $value;
					} else {
						$temp[$key] = $value;
					}

				}

				$userArray[] = $temp;

			}

			$this->set('users', $userArray);

		} 

	}
	
	function add($id = null) {
		$this->checkSession();

		$this->layout = 'ajax';

		if (!empty($this->data)){
			$this->data['User']['usr_password'] = Security::hash($this->data['User']['usr_password']);

			if ($this->User->save($this->data)){

				$this->redirect('/users'); 

			}

		} else {

			$this->set('id', $id);

		}

	}
	
	function edit($id = null) {
		$this->checkSession();

		$this->layout = 'ajax';

		$this->User->id = $id;

		

		if (empty($this->data)){
			$this->data = $this->User->read("usr_codigo, usr_nombre, usr_profile, usr_estado");
			$this->set('id', $id);

		}else{
			if (!is_null($this->data['User']['usr_password'])) {
				$this->data['User']['usr_password'] = Security::hash($this->data['User']['usr_password']);
			} 
			
			if ($this->User->save($this->data)){
				$this->redirect('/users'); 
			} else {
				$this->Session->setFlash('Error al editar el Item');
			}

		}

	}

	

	function view($id = null) {

		$this->checkSession();

		$this->layout = 'ajax';

		$this->User->id = $id;

		

		if (empty($this->data)){

			$this->data = $this->User->read();

			$this->set('id', $id);

		}

	}

	

	function delete($id) {

		$this->checkSession();

		if ($this->User->del($id)) { 

			$this->set('error', '0');

		} else {

			$this->set('error','1');

		}

		$this->redirect('/users');

	

	}
	
	function beforeFilter() { 
		$this->__validateLoginStatus(); 
	} 
     
	function login() { 
		$this->layout = 'login';
		$this->set('error', false); 
		if(!empty($this->data)) { 
			if(($user = $this->User->validateLogin($this->data['User']))) { 
				$this->Session->write('User', $user); 
				$usr_profile = $this->User->getUserProfile($user['usr_codigo']);
				$this->Session->write('Profile', $usr_profile);
				if ($usr_profile == 'inspector') {
					$sur_codigo = $this->User->getSurveyorId($user['usr_codigo']);
					$this->Session->write('Surveyor', $sur_codigo);	
				}
				$this->redirect('/'); 
			} else {
				$this->set('error', true); 
				$this->Session->setFlash('El inicio de sesion es incorrecto. Por favor intente nuevamente.'); 
			}
		}      
	} 
 
	function logout() {
		$this->Session->destroy('User');
		$this->Session->setFlash('Has cerrado sesion correctamente.');
		$this->redirect('/users/login'); 

	}
         
    function __validateLoginStatus() 
    { 
        if($this->action != 'login' && $this->action != 'logout') 
        { 
            if($this->Session->check('User') == false) 
            { 
                $this->redirect('/users/login'); 
                $this->Session->setFlash('The URL you\'ve followed requires you login.'); 
            } 
        } 
    } 
     
} 

?>